<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Uploadimg extends Base{
    public function qiniuupload(){
        $sysres= Db::name('system')->where('id',1)->find();
        if ($sysres['file_state']==0) {
            $file=$_FILES['file'];
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
            return json(['code' => 200, 'data' =>config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $bucket = $sysres['bucket'];
            $token = $auth->uploadToken($bucket);
            $filePath =$_FILES['file']['tmp_name'];
            $key = $_FILES['file']['name'];
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            return json(['code' => 200, 'data' =>$sysres['url'].'/'.$_FILES['file']['name'], 'msg' => '成功']);
        }
    }
}